
/**
 * GoodDetailPicShowerActivity.java
 * @author Jason Lu
 * @date 2013-10-22
 * @version 1.0
 */
package com.x52im.mall.logic.shop;

import java.util.ArrayList;

import android.app.Activity;
import android.content.Context;
import android.graphics.Bitmap;
import android.os.Bundle;
import android.util.AttributeSet;
import android.util.Log;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.Gallery;
import android.widget.ImageView;
import android.widget.LinearLayout.LayoutParams;
import android.widget.Toast;

import com.x52im.mall.shop.dto.Pic;
import com.x52im.rbchat.R;
import com.x52im.mall.MallGlobal;
import com.eva.android.widget.AListAdapter2;
import com.eva.android.widget.AsyncBitmapLoader;
import com.eva.android.widget.AsyncBitmapLoader.ImageCallBack;
import com.eva.android.widget.BaseActivity;

/**
 * @author Jason
 * 
 */
public class GoodDetailPicShowerActivity extends BaseActivity
{
	/** 画廊（用于显示对应颜色的各图片） */
	private Gallery galeryPics = null;

	/** 画廊Adapter（数据模型与当前选中的颜色所相关的图片数据模型对应） */
	private PicsOfColorGalleryAdapter galleryAdapter = null;

	/** 画廊中当前选是的图片索引值（本字段将在画廊的图片选中事件被触发时更新） */
	private static int currentSelectedPicIndex = 0;
	
	private ArrayList<Pic> picOfColor;
	
	@Override
	protected void onCreate(Bundle savedInstanceState)
	{
		// TODO Auto-generated method stub
		super.onCreate(savedInstanceState);
		setContentView(R.layout.common_mall_shop_layout_good_detail_pic_shower);
		
		initDataFromIntent();
		initViews();
		initListeners();
		
	}

	private void initDataFromIntent()
	{
		//解析从intent中传过来的数据
		picOfColor = (ArrayList<Pic>) getIntent().getSerializableExtra("PicOfColor");
		currentSelectedPicIndex = getIntent().getIntExtra("position", currentSelectedPicIndex);
	}
	
	private void initViews()
	{
		galeryPics = (Gallery) findViewById(R.id.common_mall_shop_layout_good_detail_shower_goodPicsGalery);
		// 设置gallery的adapter
		galleryAdapter = new PicsOfColorGalleryAdapter(GoodDetailPicShowerActivity.this);
		galleryAdapter.setListData(picOfColor);
		galeryPics.setAdapter(galleryAdapter);
		
		galeryPics.setSelection(currentSelectedPicIndex);
	}

	private void initListeners()
	{
		/**
		 * 初始化各组件的事件监听者.
		 */
		// 滑动gallery的图片时更新对应指示器的选中样式
		galeryPics.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener()
		{
			@Override
			public void onItemSelected(AdapterView<?> parent, View v, int position, long id)
			{
				// 保存当前被选中的索引
				currentSelectedPicIndex = position;
			}

			@Override
			public void onNothingSelected(AdapterView<?> arg0)
			{
				// 这里不做响应
			}
		});
		
		galeryPics.setOnItemClickListener(new AdapterView.OnItemClickListener()
		{

			@Override
			public void onItemClick(AdapterView<?> arg0, View arg1, int arg2, long arg3)
			{
				GoodDetailPicShowerActivity.this.finish();
			}
		});
	}

	/**
	 * 商品图片明细适配器实现类.
	 * 
	 * @author Jason
	 * @version 1.0
	 */
	private static class PicsOfColorGalleryAdapter extends AListAdapter2<Pic>
	{
		private AsyncBitmapLoader asyncLoader = null;
		/** 记录选中的ListView的行索引值以备后用（目前是在：单击、长按2类事件中保存了此索引值）. */
		private int selectedListViewIndex = -1;

		public PicsOfColorGalleryAdapter(Activity context)
		{
			// R.layout.chatting_list_view_bottom_waves_list_item
			super(context, R.layout.common_mall_shop_layout_good_detail_pic_item2);
			this.asyncLoader = new AsyncBitmapLoader(MallGlobal.getMallProviderInstance(
					context).getRobotimeMall().getGoodPicsCachedDirPath());
		}

		@Override
		public View getView(final int position, View convertView, ViewGroup parent)
		{
			try
			{
				// 列表item上的：
//				TextView viewText = null;
				// 列表item上的：
				ImageView viewImage = null;

				boolean needCreateItem = (convertView == null);
				// 正在操作的列表行的数据集
				final Pic rowData = listData.get(position);
				if (needCreateItem)
					// 明细item的UI实例化
					convertView = layoutInflater.inflate(itemResId, null);
				// item里的操作组件实例
//				viewText = (TextView) convertView.findViewById(R.id.common_mall_shop_layout_good_detail_pic_item_textView);
				viewImage = (ImageView) convertView.findViewById(R.id.common_mall_shop_layout_good_detail_pic_item_imageView);

//				viewText.setText(rowData.getPic_desc());
				Bitmap bitmap = asyncLoader.loadBitmap(viewImage, MallGlobal.getMallProviderInstance(this.getContext()).getRobotimeMall().GOODS_PIC_HTTP_ROOT_URL() + rowData.getPic_path(), new ImageCallBack()
				{
					@Override
					public void imageLoad(ImageView imageView, Bitmap bitmap)
					{
						imageView.setImageBitmap(bitmap);
					}
				});

				if (bitmap == null)
				{
					viewImage.setImageResource(R.drawable.common_mall_default_pic);
				}
				else
				{
					viewImage.setImageBitmap(bitmap);
				}
			}
			catch (OutOfMemoryError e)
			{
				Log.w(GoodDetailActivity.class.getSimpleName(), "商品图片显示内存不足,建议退出程序并重新进入.", e);
				Toast.makeText(getContext(), "商品图片显示内存不足,建议退出程序并重新进入.", Toast.LENGTH_SHORT).show();
				// TODO: out of memory deal..
			}

			return convertView;
		}

		@SuppressWarnings("unused")
		public int getSelectedListViewIndex()
		{
			return selectedListViewIndex;
		}

		@SuppressWarnings("unused")
		public void setSelectedListViewIndex(int selectedListViewIndex)
		{
			this.selectedListViewIndex = selectedListViewIndex;
		}
	}

	private static class IndicatorImageView extends ImageView
	{
		public IndicatorImageView(Context context)
		{
			this(context, null);
		}

		public IndicatorImageView(Context context, AttributeSet attrs)
		{
			super(context, attrs);

			init();
		}

		private void init()
		{
			LayoutParams mlp = new LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT);
			// this.setMargins(5, 5, 5, 5); // setMargins don't work
			this.setPadding(5, 5, 5, 5); // but setPadding is ok
			this.setLayoutParams(mlp);
			// default status
			this.setIndicatorSelected(false);
		}

		public void setIndicatorSelected(boolean selected)
		{
			if (selected)
				this.setImageResource(R.drawable.common_mall_guide_dot_black);
			else
				this.setImageResource(R.drawable.common_mall_guide_dot_white);
		}
	}
}
